package qmlt.dataset.filter;

import java.util.ArrayList;
import java.util.List;

import qmlt.dataset.Attribute;
import qmlt.dataset.DataSetBase;
import qmlt.dataset.DataSet;
import qmlt.dataset.Instance;
import qmlt.dataset.utils.DataSetUtils;
import qmlt.dataset.utils.ListUtils;

public class MissingValueFilter implements Filter
{

	@Override
	public DataSet filter(DataSet ds)
	{
		List<Object> replaces = new ArrayList<Object>();
		for (int i = 0; i < ds.getFeatureDefs().size(); ++i)
		{
			if (ds.getFeatureDefs().get(i).type.equals(Attribute.STRING))
			{
				replaces.add(dealWithStringColumn(ds, i));
			}
			else if (ds.getFeatureDefs().get(i).type.equals(Attribute.FLOAT))
			{
				replaces.add(dealWithFloatColumn(ds, i));
			}
		}

		DataSet newds = new DataSetBase(ds.getDef().clone(ds.getId() + "-missing"), ds);
		for (Instance inst : ds.getInstances())
		{
			Instance newInst = inst.shallowClone(newds);
			for (int i = 0; i < inst.getFeatures().size(); ++i)
			{
				if (newInst.getFeatures().get(i) == null)
				{
					newInst.getFeatures().set(i, replaces.get(i));
				}
			}
		}

		return newds;
	}

	private Object dealWithFloatColumn(DataSet ds, int iCol)
	{
		List<Float> values = ListUtils.convert(DataSetUtils.getFeatureColumn(ds.getInstances(), iCol));
		return new Float(ListUtils.mean(values));
	}

	private Object dealWithStringColumn(DataSet ds, int iCol)
	{
		List<String> values = ListUtils.convert(DataSetUtils.getFeatureColumn(ds.getInstances(), iCol));
		return ListUtils.findMajor(values);
	}
}
